package son.hut.vn.app.network;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import son.hut.vn.app.infor.WebserviceConfig;
import son.hut.vn.app.utility.SmartLogUtility;

import android.content.Context;
import android.util.Log;

/**
 * AsyncHttpGet makes http get request based on AsyncTask
 * 
 * @author sonnv6207
 */
public class AsyncHttpGet extends AsyncHttpBase
{
    /**
     * Constructor
     * 
     * @param context
     * @param listener
     * @param parameters
     */
    public AsyncHttpGet(Context context, AsyncHttpResponseListener listener,  List<NameValuePair> parameters)
    {
        super(context, listener, parameters);
    }

    /**
     * Constructor
     * @param context
     * @param process
     * @param parameters
     */
    public AsyncHttpGet(Context context, AsyncHttpResponseProcess process, List<NameValuePair> parameters)
    {
        super(context, process, parameters);
    }
    /*
     * com.seta.medtalkdroid.app.network.AsyncHttpBase#request(java.lang.String)
     */
    @SuppressWarnings("deprecation")
	@Override
    protected String request(String url)
    {
        try
        {
            HttpParams params = new BasicHttpParams();
            if (parameters != null)
            {
                Iterator<NameValuePair> it = parameters.iterator();
                int i = 0;
                while (it.hasNext())
                {
                    NameValuePair nv = it.next();
                    params.setParameter(nv.getName(), nv.getValue());
                    if (i == 0) {
                        url += "?" + URLEncoder.encode(nv.getName()) + "=" + URLEncoder.encode(nv.getValue());
                    } else {
                        url += "&" + URLEncoder.encode(nv.getName()) + "=" + URLEncoder.encode(nv.getValue());
                    }
                    i++;
                }
            }
            HttpConnectionParams.setConnectionTimeout(params, WebserviceConfig.NETWORK_TIME_OUT);
            HttpConnectionParams.setSoTimeout(params, WebserviceConfig.NETWORK_TIME_OUT);

            HttpClient httpclient = createHttpClient(url, params);
            HttpGet httpget = new HttpGet(url);
            
            SmartLogUtility.log("method request: ", httpget.getMethod());
            SmartLogUtility.log("request URI: ", httpget.getURI().toString());
            SmartLogUtility.log("request Line: ", httpget.getRequestLine().toString());
            
            try
            {
                //Execute Request
                response = httpclient.execute(httpget);
                statusCode = NETWORK_STATUS_OK;
                CAUSE_ERROR = "";
            }catch(java.net.SocketTimeoutException ex)
            {
                statusCode = NETWORK_STATUS_TIMEOUT;
                ex.printStackTrace();
            }catch(javax.net.ssl.SSLException ex){
                statusCode = NETWORK_STATUS_ERROR;
                ex.printStackTrace();
            }catch(org.apache.http.conn.ConnectTimeoutException timeoutEx){
                statusCode = NETWORK_STATUS_TIMEOUT;
                timeoutEx.printStackTrace();
            }catch(ClientProtocolException clientEx){
                //ClientProtocolException - in case of an http protocol error
                statusCode = NETWORK_STATUS_ERROR;
                clientEx.printStackTrace();
            }catch(IOException ioEx){
              //IOException - in case of a problem or the connection was aborted
                statusCode = NETWORK_STATUS_ERROR;
                ioEx.printStackTrace();
            }
            Log.d("STATUSCODE","HttpGet:" + statusCode);
        }
        catch (Exception e)
        {
            statusCode = NETWORK_EXCEPTION;
            CAUSE_ERROR = e.getMessage();
            Log.d("HTTP_GET_ERROR",e.getMessage());
            e.printStackTrace();
        }
        return null;
    } 
}
